<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList1" @findClicked="findData" @Launchform="Launchform" />

    <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList" @findClicked="findData1" @Launchform="Launchform1" />
    <el-card style="margin-top: 7px" shadow="always" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <div style="width: 100%">
          <h1>{{ $t(`Print.InquireAboutTheWarehousingDetails`) }}</h1>
          <Table
            :summaryData="summaryData"
            summary
            :tableHeader="tableHeader"
            :tableData="QeryTheSorgeTableDateList.dtos"
            :TableHeight="100"
            :outTableHeight="55"
            :isLoading="isLoading"
            @currentRow="clicktogetdata"
            @confirmChange="confirmChange"
            :getTableList="QueryTheStorge"
            @selectValChange="confirmChange7"
          />
        </div>
      </div>
      <PageModule :PageData="{ total }" @pageChange="handleSizeChangeT" @pageSizeChange="handleCurrentChangeT" />
    </el-card>

    <el-drawer v-model="drawer1" :direction="'ttb'" size="small" :with-header="false">
      <Find
        :OriginalFilter="{ static: true }"
        style="margin-bottom: 20px"
        :searchImg="false"
        :formList="formList2"
        @findClicked="findData2"
        @Launchform="Launchform2"
      />

      <h1 style="font-size: 16px; margin-bottom: 10px">{{ $t(`Print.MaterialSubstitutionInquiry`) }}</h1>
      <Table
        @dblclick="TwoClick"
        :tableHeader="tableHeader1"
        :tableData="MaterialSubstitutionQueryTableDateList"
        :TableHeight="70"
        :isLoading="isLoading1"
        @currentRow="clicktogetdataLta"
        @confirmChange="confirmChangeT"
        :getTableList="MaterialSubstitutionQueryTableList"
        
      />
    </el-drawer>

    <el-drawer v-model="drawer2" :direction="'ttb'" size="small" :with-header="false">
      <Find
        :OriginalFilter="{ static: true }"
        style="margin-bottom: 20px"
        :searchImg="false"
        :formList="formList3"
        @findClicked="findData3"
        @Launchform="Launchform3"
      />

      <h1 style="font-size: 16px; margin-bottom: 10px">{{ $t(`Print.MaterialSubstitutionInquiry`) }}</h1>
      <Table
        style="width: 70%"
        :tableHeader="tableHeader2"
        :tableData="SubstituteMaterialTableDateList.dtos"
        :TableHeight="70"
        :isLoading="isLoadingT"
        @currentRow="clicktogetdataLtaLack"
      />
      <PageModule :PageData="{ total: totalT }" @pageChange="handleSizeChange" @pageSizeChange="handleCurrentChange" />
    </el-drawer>

    <el-dialog v-model="drawer10" :direction="'ttb'" size="small" :with-header="false">
      <Find
        :OriginalFilter="{ static: true }"
        style="margin-bottom: 20px"
        :searchImg="false"
        :formList="formList5"
        @Launchform="Launchform5"
      />
     <el-button type="primary" size="default" @click="addMethods">新增</el-button>
     <el-button type="primary" size="default" @click="deleteMethods">删除</el-button>
     
      <Table
        style="width: 70%"
        :tableHeader="tableHeader10"
        :tableData="CustomsCodeQueryTableDate1"
        :TableHeight="70"
        @currentRow="clicktogetdataLtaLack1"
        @inputChange="inputChange"
      />

    </el-dialog>

    <el-dialog v-model="drawer3" :direction="'ttb'" size="small" :with-header="false">
      <Find
        :OriginalFilter="{ static: true }"
        style="margin-bottom: 20px"
        :searchImg="false"
        :formList="formList4"
        @findClicked="findData4"
        @Launchform="Launchform4"
      />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'MaterialWarehousingDeletion'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref, onMounted } from 'vue'
import {
  QueryTheStorageDetails,
  BlockDeletion,
  CurrentLine,
  QuantityCurrentlyAccepted,
  VendorQuery,
  MaterialSubstitutionQuery,
  confirmModification,
  QueryWithMaterialsInstead,
  MaterialReplacementTreatment,
  MaterialSubstitutionAddition,
  CustomsCodeQuery,
  CustomsCodeAddition,
  CustomsCodeDeletion,
CustomsCodeQuery1
} from '@/api/WarehouseDepartment/FactoryStorage/MaterialWarehousingDeletion'
import type {
  QueryTheStorageDetailsInterface,
  QueryTheStorageParms,
  CurrentLineParms,
  MaterialSubstitutionQueryParms,
  MaterialSubstitutionQueryInterface,
  confirmModificationParms,
  QueryWithMaterialsInsteadParms,
  QueryWithMaterialsInsteadInterface,
  MaterialReplacementTreatmentParms,
  MaterialSubstitutionAdditionParms
} from '@/api/WarehouseDepartment/FactoryStorage/MaterialWarehousingDeletion'
import { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'
const inputChange = async(row:any) => {
  const result = await LoginWork.put<any,any>(`/hgMtl/update`,row)
  if (result.code == 200) {
    ElMessage.success(result.message)
    CustomsCodeQueryMethods1() 
  }else{
    ElMessage.error(result.message)
  }
}

// 海关编码删除
const CustomsCodeDeletionParms = ref('')
const CustomsCodeDeletionMethods = async() => {
  const result = await CustomsCodeDeletion(CustomsCodeDeletionParms.value) 
  if (result.code == 200) {
    ElMessage.success(result.message)
    CustomsCodeQueryMethods1() 
  }else{
    ElMessage.error(result.message)
  }
}
const handleCloseMateriaLKlc = (_done?: () => void) => {
  if (CustomsCodeDeletionParms.value === '') {
    JMessage.warning('请点击需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        CustomsCodeDeletionMethods()
      })
      .catch(() => {})
  }
}
const clicktogetdataLtaLack1 = (row:any) => {
  CustomsCodeDeletionParms.value = row.id
}
const formList4 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入海关编码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入英文名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '确认',
    bgColor: '#1890FF'
  },
])
const formList5 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入海关编码',
    inputValue: ''
  },
])
const findData4 = (row:any) => {
  if(row === '确认'){
    if(!CustomsCodeAdditionParms.value.mtlNameEng || !CustomsCodeAdditionParms.value.mtlNoHg){
      ElMessage.warning('请输入完整信息')
      return 
    }
    CustomsCodeAdditionMethods()
  }
}
const Launchform4 = (row:any) => {
  CustomsCodeAdditionParms.value.mtlNameEng = row[1].inputValue
  CustomsCodeAdditionParms.value.mtlNoHg = row[0].inputValue
}
const CustomsCodeAdditionParms = ref({
    id:"",
    mtlNameEng:"",
    mtlNoHg:"",
    type:""
})
const drawer3 = ref(false)
const Launchform5 = (row:any) => {
  parmsParms.value = row[0].inputValue
  CustomsCodeQueryMethods1()
}
const CustomsCodeAdditionMethods = async () => {
  const result = await CustomsCodeAddition(CustomsCodeAdditionParms.value) 
  if (result.code == 200) {
    formList4.value[0].inputValue = ''
    formList4.value[1].inputValue = ''
    drawer3.value = false
    ElMessage.success(result.message)
    CustomsCodeQueryMethods1() 
  }else{
    ElMessage.error(result.message)
  }
}

const addMethods = () => {
  drawer3.value = true
}

const deleteMethods = () => {
  handleCloseMateriaLKlc()
}

const confirmChange7 = async(row:any) => {
  
  row.hgId = row.mtlNoHg.selectValue.split(' ')[0]
  row.mtlNoHg = ''
  const result = await LoginWork.put<any, any>('/pcFactorStockQuery/mtlNoHgUpdate',row)
  if (result.code == 200) {
    QueryTheStorge()
    ElMessage.success(result.message)
  }else{
    ElMessage.error(result.message)
  }
}
// 海关编码查询
const CustomsCodeQueryMethods = async () => {
  const result = await CustomsCodeQuery()
  CustomsCodeQueryTableDate.value = EvaluatesTheArrayReturnValue1(result.map((item:any) => {
    return item.id + ' ' + item.mtlNoHg +  ' ' + item.mtlNameEng 
  }))
}
const parmsParms = ref('')
const CustomsCodeQueryMethods1 = async () => {
  const result = await CustomsCodeQuery1(parmsParms.value)
  CustomsCodeQueryTableDate1.value = result.map((item:any) => {
    item.change = 'Y'
    item.disableConfirm = true
    return item
  })
}

const CustomsCodeQueryTableDate = ref([])
const CustomsCodeQueryTableDate1 = ref([])

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}


const formList1 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入入库单号',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },

])

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入删除入库单号',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '整单删除'
  },
  {
    type: 'button',
    checkboxLabel: '当前行删除'
  },
  {
    type: 'input',
    inputPlaceholder: '请输入现暂收数量',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '数量修改'
  },
  {
    type: 'date',
    dateLabel: '送货日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入送货单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '确认修改'
  },
  {
    type: 'button',
    checkboxLabel: '材料替代'
  },
  {
    type: 'button',
    checkboxLabel: '材料替代查询'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '海关编码编辑',
    bgColor: '#1890FF'
  }
])

const formList2 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入入库单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称(替代前)',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称(替代后)',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '材料替换处理'
  },
  {
    type: 'button',
    checkboxLabel: '库存明细查询'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },

])

const formList3 = ref([
  {
    type: 'input',
    inputPlaceholder: '分类',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '确认'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const rowTableDateList = ref()

const active = ref(false)

// 修改
const modificationFormList = ref()

const confirmChangeTParms = ref({
  mtlColor: '',
  mtlName: '',
  mtlNo: '',
  mtlSpec: '',
  mtlUnit: '',
  stkQty: ''
})

const clicktogetdataLtaLack = (row: any) => {
  rowTableDateList.value = row
  const { mtlColor, mtlName, mtlNo, mtlSpec, mtlUnit, stkQty } = row
  confirmChangeTParms.value.mtlColor = mtlColor
  confirmChangeTParms.value.mtlName = mtlName
  confirmChangeTParms.value.mtlNo = mtlNo
  confirmChangeTParms.value.mtlSpec = mtlSpec
  confirmChangeTParms.value.mtlUnit = mtlUnit
  confirmChangeTParms.value.stkQty = stkQty
}

const TwoClick = () => {
  SubstituteMaterial()
  drawer2.value = true
}

// const PcFactorStockQuer = ref({
//   recNo:'',
//   lotNo:'',
//   mtlNo:'',
//   mtlNoUp:'',
//   mtlName:'',
//   mtlColor:'',
//   mtlSpec:'',
//   mtlUnit:'',
//   qty:'',
//   qtyUp:'',
//   mtlNo2:'',
//   mtlNo2Up:'',
//   mtlName2:'',
//   mtlColor2:'',
//   mtlSpec2:'',
//   mtlUnit2:'',
//   qty2:'',
//   qty2Up:'',
// })

// 新增材料替代
const NewMaterialParms = ref<MaterialSubstitutionAdditionParms>({
  lotNo: '',
  mtlColor: '',
  mtlName: '',
  mtlNo: '',
  mtlSpec: '',
  mtlUnit: '',
  recNo: '',
  sendQty: '',
  companyId: ''
})

const NewMaterial = async () => {
  const result = await MaterialSubstitutionAddition(NewMaterialParms.value)
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 材料替代
const materialSubstitutionParms = ref<MaterialReplacementTreatmentParms>({
  lotNo: '',
  mtlColor: '',
  mtlColor2: '',
  mtlName: '',
  mtlName2: '',
  mtlNo: '',
  mtlNo2: '',
  mtlSpec: '',
  mtlSpec2: '',
  mtlUnit: '',
  mtlUnit2: '',
  outType: '',
  qty: 0,
  qty2: 0,
  slipNo: '',
  stkQty: 0
})

const clicktogetdataLta = (row: any) => {
  modificationFormList.value = row
  const { mtlColor2, mtlName2, mtlNo2, mtlSpec2, mtlUnit2, outType, qty2, slipNo, mtlNo, qty, lotNo, mtlColor, mtlName, mtlSpec, mtlUnit, stkQty } =
    row
  materialSubstitutionParms.value.lotNo = lotNo
  materialSubstitutionParms.value.mtlColor = mtlColor
  materialSubstitutionParms.value.mtlColor2 = mtlColor2
  materialSubstitutionParms.value.mtlName = mtlName
  materialSubstitutionParms.value.mtlName2 = mtlName2
  materialSubstitutionParms.value.mtlNo = mtlNo
  materialSubstitutionParms.value.mtlNo2 = mtlNo2
  materialSubstitutionParms.value.mtlSpec = mtlSpec
  materialSubstitutionParms.value.mtlSpec2 = mtlSpec2
  materialSubstitutionParms.value.mtlUnit = mtlUnit
  materialSubstitutionParms.value.mtlUnit2 = mtlUnit2
  materialSubstitutionParms.value.outType = outType
  materialSubstitutionParms.value.qty = qty
  materialSubstitutionParms.value.qty2 = qty2
  materialSubstitutionParms.value.slipNo = slipNo
  materialSubstitutionParms.value.stkQty = stkQty
}

const confirmChangeT = async (row?: any, row2?: any) => {
  const { mtlColor, mtlName, mtlSpec, mtlUnit, stkQty, mtlNo } = row2

  const result = await LoginWork.put<any, any>(
    '/pcFactorStockQuery/mtlSubUpdate',
    [row].map((item) => {
      return {
        recNo: item.slipNo,
        lotNo: item.lotNo,
        mtlNo: materialSubstitutionParms.value.mtlNo,
        mtlNoUp: item.mtlNo,
        mtlName: item.mtlName,
        mtlColor: item.mtlColor,
        mtlSpec: item.mtlSpec,
        mtlUnit: item.mtlUnit,
        qty: materialSubstitutionParms.value.qty,
        qtyUp: Number(item.qty),

        mtlName2: active.value === true ? mtlName : item.mtlName2,
        mtlColor2: active.value === true ? mtlColor : item.mtlColor2,
        mtlSpec2: active.value === true ? mtlSpec : item.mtlSpec2,
        mtlUnit2: active.value === true ? mtlUnit : item.mtlUnit2,

        // mtlNo2:mtlNo,
        mtlNo2: materialSubstitutionParms.value.mtlNo2,
        mtlNo2Up: active.value === true ? mtlNo : item.mtlNo2,

        qty2: materialSubstitutionParms.value.qty2,
        qty2Up: active.value === true ? stkQty : item.qty2
      }
    })
  )
  if (result.code === 200) {
    drawer2.value = false
    JMessage.success(result.message)
    active.value = false
    MaterialSubstitutionQueryTableList()
  } else {
    active.value = false
    JMessage.error(result.message)
  }
}

const handleCloseFiveMaterials = (_done?: () => void) => {
  ElMessageBox.confirm('请确认要材料替换处理吗？')
    .then(() => {
      MaterialSubstitutionQueryTableList()
      NewMaterial()
      drawer1.value = true
    })
    .catch(() => {})
}

const materialSubstitution = async () => {
  const result = await MaterialReplacementTreatment(materialSubstitutionParms.value)
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 替代用材料查询
const SubstituteMaterialTableDateList = ref<QueryWithMaterialsInsteadInterface>({
  dtos: []
})

const isLoadingT = ref(true)

const drawer2 = ref(false)

const SubstituteMaterialParms = ref<QueryWithMaterialsInsteadParms>({
  lotNo: '',
  mtlName: '',
  mtlType: '',
  pageNum: 1,
  pageSize: 40
})

const SubstituteMaterial = async () => {
  isLoadingT.value = true
  const result = await QueryWithMaterialsInstead(SubstituteMaterialParms.value)
  totalT.value = result.total!
  SubstituteMaterialTableDateList.value.dtos = result.dtos
  isLoadingT.value = false
}

// 修改后点击确定按钮
const confirmChange = async (row: any) => {
  const result = await LoginWork.put<any, any>(`/pcFactorStockQuery/otherDateUpdate/${row.recNo}`, row)
  if (result.code === 200) {
    QueryTheStorge()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 确认修改后的数据
const confirmModificationButtonParms = ref<confirmModificationParms>({
  kgQtyUp: '',
  sendDateUp: '',
  sendNoUp: '',
  vendorNameUp: '',
  vendorNoUp: '',
  mtlColor: '',
  mtlName: '',
  purLot: '',
  purNo: '',
  recNo: '',
  sendNo: '',
  sendQty: '',
  slipType: '',
  vendorName: ''
})

const confirmModificationButtonQuery = ref<string>('')

const confirmModificationButton = async () => {
  const result = await confirmModification(confirmModificationButtonParms.value, confirmModificationButtonQuery.value)
  if (result.code === 200) {
    JMessage.success(result.message)
    QueryTheStorge()
  } else {
    JMessage.error(result.message)
  }
}

interface summaryDataType {
  index: number
  num: number | undefined
}

// 计算金额总数
const summaryData = ref<summaryDataType[]>([
  {
    index: 13,
    num: 0
  }
])

// 材料替代查询

const MaterialSubstitutionQueryTableListParms = ref<MaterialSubstitutionQueryParms>({
  lotNo: '',
  mtlName: '',
  mtlName2: '',
  recNo: ''
})

const MaterialSubstitutionQueryTableDateList = ref<MaterialSubstitutionQueryInterface[]>()

const MaterialSubstitutionQueryTableList = async () => {
  isLoading1.value = true
  const result = await MaterialSubstitutionQuery(MaterialSubstitutionQueryTableListParms.value)
  MaterialSubstitutionQueryTableDateList.value = result
  result.forEach((item: any) => {
    if (item.outType !== '替换完成') {
      item.change = 'Y'
    }
  })
  isLoading1.value = false
}

const isLoading1 = ref(true)

// 材料替代
const drawer1 = ref<boolean>(false)

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

// 厂商数据
const VendorQueryDateList = async () => {
  let NewArray = []
  const result = await VendorQuery()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorNo + ' ' + item.vendorName)
    }
    return arr
  }, [])
  formList.value[7].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

// 数量修改
const sendQtyNumber = ref<string>()

const sendQtyNumberId = ref('')

const QuantityModification = async () => {
  const result = await QuantityCurrentlyAccepted(sendQtyNumberId.value, Number(sendQtyNumber.value!))
  if (result.code === 200) {
    JMessage.success(result.message)
    sendQtyNumber.value = ''
    // BlockDeletionDelete()
    QueryTheStorge()
  } else {
    sendQtyNumber.value = ''
    JMessage.error(result.message)
  }
}

// 整行删除
const CurrentLineQuery = ref<any>('')

const CurrentLineDelete = async () => {
  const result = await CurrentLine(CurrentLineQuery.value)
  if (result.code === 200) {
    JMessage.success(result.message)
    QueryTheStorge()
  } else {
    JMessage.error(result.message)
  }
}

const handleCloseFive = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(() => {
      CurrentLineDelete()
    })
    .catch(() => {})
}

const clicktogetdata = (row: any) => {
  const { mtlColor, mtlName, purNo, recNo, sendNo, sendQty, slipType, vendorName, purLot, mtlNo, mtlSpec, mtlUnit, qty, companyId, reqNo } = row
  CurrentLineQuery.value = row.primaryId

  confirmModificationButtonParms.value.mtlColor = mtlColor
  confirmModificationButtonParms.value.mtlName = mtlName
  confirmModificationButtonParms.value.purLot = purLot
  confirmModificationButtonParms.value.purNo = purNo
  confirmModificationButtonParms.value.recNo = recNo
  confirmModificationButtonParms.value.sendNo = sendNo
  confirmModificationButtonParms.value.sendQty = sendQty
  confirmModificationButtonParms.value.slipType = slipType
  confirmModificationButtonParms.value.vendorName = vendorName
  confirmModificationButtonQuery.value = recNo

  materialSubstitutionParms.value.lotNo = purLot
  materialSubstitutionParms.value.mtlColor = mtlColor
  materialSubstitutionParms.value.mtlName = mtlName
  materialSubstitutionParms.value.mtlNo = mtlNo
  materialSubstitutionParms.value.mtlSpec = mtlSpec
  materialSubstitutionParms.value.mtlUnit = mtlUnit
  materialSubstitutionParms.value.qty = qty

  NewMaterialParms.value.lotNo = purLot
  NewMaterialParms.value.mtlColor = mtlColor
  NewMaterialParms.value.mtlName = mtlName
  NewMaterialParms.value.mtlNo = mtlNo
  NewMaterialParms.value.mtlSpec = mtlSpec
  NewMaterialParms.value.mtlUnit = mtlUnit
  NewMaterialParms.value.recNo = recNo
  NewMaterialParms.value.sendQty = sendQty
  NewMaterialParms.value.companyId = companyId

  sendQtyNumberId.value = row.primaryId
}

// 整单删除
const BlockDeletionQuery = ref<string>('')

const BlockDeletionDelete = async () => {
  const result = await BlockDeletion(BlockDeletionQuery.value)
  if (result.code === 200) {
    JMessage.success(result.message)
    QueryTheStorge()
    formList1.value[4].inputValue = ''
  } else {
    JMessage.error(result.message)
    formList1.value[4].inputValue = ''
  }
}

const handleClose3 = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(() => {
      BlockDeletionDelete()
    })
    .catch(() => {})
}

// 入库明细查询
const isLoading = ref(true)

const QueryTheStorgeParms = ref<QueryTheStorageParms>({
  mtlName: '',
  pageNum: 1,
  pageSize: 40,
  purLot: '',
  recDateEnd: '',
  recDateStart: '',
  recNo: ''
})

const QeryTheSorgeTableDateList = ref<QueryTheStorageDetailsInterface>({
  dtos: []
})

const QueryTheStorge = async () => {
  isLoading.value = true
  const result = await QueryTheStorageDetails(QueryTheStorgeParms.value)
  if (result) {
    total.value = result.total!
    summaryData.value[0].num = result.sendQtyTotal
    QeryTheSorgeTableDateList.value.dtos = result.dtos
    if (result !== null) {
      result.dtos.forEach((item: any) => {
        if (item.cfmFlag === 'Y') {
          item.cfmFlag = true
          item.backgroundColor = '#40e080'
        }
        item.change = 'Y'
        item.mtlNoHg = {
          selectValue: item.mtlNoHg || '',
          options:CustomsCodeQueryTableDate.value
        }
      })
    }
  }
  isLoading.value = false
}

// 表格数据
const tableHeader = ref([
  {
    label: '类型',
    prop: 'slipType',
    change: true
  },
  {
    label: '海关编码',
    prop: 'mtlNoHg',
    type:'select'
  },
  {
    label: '入库单号',
    prop: 'recNo',
    change: true,
    backgroundColor: true
  },
  {
    label: '入库日期', 
    prop: 'recDate',
    change: true
  },
  {
    label: '送货日期',
    prop: 'sendDate',
    change: true
  },
  {
    label: '送货单号',
    prop: 'sendNo',
    change: true
  },
  {
    label: '订购单号',
    prop: 'purNo',
    change: true
  },
  {
    label: '厂商名称',
    prop: 'vendorName',
    change: true
  },
  {
    label: '指令',
    prop: 'purLot',
    change: true
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '公斤数',
    prop: 'kgQty'
  },
  {
    label: '暂收数量',
    prop: 'acceptQty',
    change: true
  },
  {
    label: '财务备注',
    prop: 'financeRecMemo',
    change: true
  },
  {
    label: '仓管员',
    prop: 'sysUser',
    change: true
  },
  {
    label: '财务检核',
    type: 'checkbox',
    prop: 'cfmFlag',
    disable: true
  }
])

const tableHeader1 = ref([
  {
    label: '入库单号',
    prop: 'slipNo',
    change: true
  },
  {
    label: '指令',
    prop: 'lotNo',
    change: true
  },
  {
    Multistage: true,
    label: '材料替代前',
    children: [
      {
        label: '物料编码',
        prop: 'mtlNo'
      },
      {
        label: '材料名称',
        prop: 'mtlName'
      },
      {
        label: '材料颜色',
        prop: 'mtlColor'
      },
      {
        label: '规格',
        prop: 'mtlSpec'
      },
      {
        label: '单位',
        prop: 'mtlUnit'
      },
      {
        label: '数量',
        prop: 'qty'
      }
    ]
  },

  {
    Multistage: true,
    label: '材料替代后',
    children: [
      {
        label: '物料编码',
        prop: 'mtlNo2'
      },
      {
        label: '材料名称',
        prop: 'mtlName2'
      },
      {
        label: '材料颜色',
        prop: 'mtlColor2'
      },
      {
        label: '规格',
        prop: 'mtlSpec2'
      },
      {
        label: '单位',
        prop: 'mtlUnit2'
      }
    ]
  },

  {
    label: '数量',
    prop: 'qty2'
  },
  {
    label: '处理完成',
    prop: 'outType'
  }
])
const tableHeader10 = ref([
  {
    label: '海关编码',
    prop: 'mtlNoHg'
  },
  {
    label: '英文名称',
    prop: 'mtlNameEng'
  },
])
const tableHeader2 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '库存量',
    prop: 'stkQty'
  }
])

// 统一调用接口
const interAceFunct = () => {
  // 入库明细查询
  QueryTheStorge()

  // 厂商
  VendorQueryDateList()
}

onMounted(() => {
  interAceFunct()

  CustomsCodeQueryMethods()

  CustomsCodeQueryMethods1()
})

// 分页器
const total = ref(0)

const handleSizeChangeT = (val: number) => {
  QueryTheStorgeParms.value.pageNum = val
  QueryTheStorge()
}

const handleCurrentChangeT = (val: number) => {
  QueryTheStorgeParms.value.pageSize = val
  QueryTheStorge()
}

// 分页器
const totalT = ref(0)

const handleSizeChange = (val: number) => {
  SubstituteMaterialParms.value.pageNum = val
  SubstituteMaterial()
}

const handleCurrentChange = (val: number) => {
  SubstituteMaterialParms.value.pageSize = val
  SubstituteMaterial()
}

const Launchform = (row: any) => {
  QueryTheStorgeParms.value.recNo = row[0].inputValue
  QueryTheStorgeParms.value.purLot = row[2].inputValue
  QueryTheStorgeParms.value.recDateStart = row[1].dateValue[0]
  QueryTheStorgeParms.value.recDateEnd = row[1].dateValue[1]
  QueryTheStorgeParms.value.mtlName = row[3].inputValue
  QueryTheStorge()
}

const Launchform1 = (row: any) => {
  BlockDeletionQuery.value = row[0].inputValue
  sendQtyNumber.value = row[3].inputValue
  confirmModificationButtonParms.value.sendDateUp = row[5].dateValue[0]
  confirmModificationButtonParms.value.sendNoUp = row[6].inputValue
  confirmModificationButtonParms.value.vendorNameUp = row[7].selectValue?.split(' ')[1]?.trim()
  confirmModificationButtonParms.value.vendorNoUp = row[7].selectValue?.split(' ')[0]?.trim()
}

const Launchform2 = (row: any) => {
  MaterialSubstitutionQueryTableListParms.value.recNo = row[0].inputValue
  MaterialSubstitutionQueryTableListParms.value.lotNo = row[1].inputValue
  MaterialSubstitutionQueryTableListParms.value.mtlName = row[2].inputValue
  MaterialSubstitutionQueryTableListParms.value.mtlName2 = row[3].inputValue
  MaterialSubstitutionQueryTableList()
}

const findData = (find: string) => {
  if (find === '置空') {
    clearFormList1()
  }
}
const drawer10 = ref(false)
const findData1 = (find: string) => {
  if(find === '海关编码编辑') {
    drawer10.value = true
  }
  if (find === '置空') {
    clearFormList()
  }
  if (find === '整单删除') {
    if (BlockDeletionQuery.value.length === 0) {
      JMessage.warning('请输入需要删除的入库单号')
    } else {
      handleClose3()
    }
  }
  if (find === '当前行删除') {
    if (CurrentLineQuery.value === '') {
      JMessage.warning('请选择需要删除的数据')
    } else {
      handleCloseFive()
    }
  }
  if (find === '数量修改') {
    if (sendQtyNumber.value === undefined || sendQtyNumberId.value === '') {
      JMessage.warning('请选择需要暂收数量的行并且暂收数量不可为空')
    } else {
      QuantityModification()
    }
  }
  if (find === '材料替代查询') {
    MaterialSubstitutionQueryTableList()
    drawer1.value = true
  }
  if (find === '确认修改') {
    if (confirmModificationButtonParms.value.recNo === '') {
      JMessage.warning('请选择需要修改的行')
    } else {
      confirmModificationButton()
    }
  }
  if (find === '材料替代') {
    if (materialSubstitutionParms.value.lotNo === '') {
      JMessage.warning('请选择需要材料替代的行')
    } else {
      handleCloseFiveMaterials()
    }
  }
}

const findData2 = (find: string) => {
  if (find === '置空') {
    clearFormList1()
  }
  if (find === '材料替换处理') {
    if (materialSubstitutionParms.value.mtlColor2 === '') {
      JMessage.warning('请选择需要替换的材料')
    } else {
      materialSubstitution()
    }
  }
  if (find === '库存明细查询') {
    drawer2.value = true
    SubstituteMaterial()
  }
}

const clearFormList1 = () => {
  formList1.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'date') {
      item.dateValue = ''
    }
    // Add other conditions for different form item types
  })
}

const clearFormList = () => {
  formList.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    if (item.type === 'date') {
      item.dateValue = ''
    }
    // Add other conditions for different form item types
  })
}

const Launchform3 = (row: any) => {
  SubstituteMaterialParms.value.mtlType = row[0].inputValue
  SubstituteMaterialParms.value.mtlName = row[1].inputValue
  SubstituteMaterialParms.value.lotNo = row[2].inputValue
  SubstituteMaterial()
}

const findData3 = (find: string) => {
  if (find === '置空') {
    clearFormList2()
  }
  if (find === '确认') {
    if (confirmChangeTParms.value.mtlNo === '') {
      JMessage.warning('请选择替换的材料')
    } else {
      isLoadingT.value = false
      active.value = true
      confirmChangeT(modificationFormList.value, rowTableDateList.value)
    }
  }
}

const clearFormList2 = () => {
  formList3.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    // Add other conditions for different form item types
  })
}
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}
</style>
